<template>
  <!-- 顶部操作栏 -->
  <div class="topTool">
    <!-- 输入 & 选择框：1.店铺ID  2.店铺名-->
    <div class="mt-4">
      <el-input v-model="input" placeholder="搜索内容" class="input-with-select">
        <template #prepend>
          <el-select v-model="select" placeholder="搜索方式" style="width: 115px;background-color: rgba(254,240,240,0.5)">
            <el-option label="店铺ID" value="1" />
            <el-option label="名称" value="2" />
          </el-select>
        </template>
        <template #append>
          <el-button :icon="Search" style="width: 50px;background-color: rgba(254,240,240,0.5)" @click="handleSearch"/>
        </template>
      </el-input>
    </div>
    <el-button plain type="danger" style="margin-left: 20px;" @click="handleAdd"><el-icon><Plus /></el-icon>添加店铺</el-button>
  </div>
  <!-- el-table数据表格组件 -->
  <el-table
      :data="userData"
      :header-cell-style="{ background: 'rgb(254,240,240,0.8)'}"
      empty-text="暂无数据"
      style="margin-top: 20px;width: 100%;"
      border
  >
    <!-- 基本信息 -->
    <el-table-column prop="hanfuShopId" label="店铺ID" sortable width="100px"></el-table-column>
    <el-table-column prop="hanfuShopName" label="名称" width="100px"></el-table-column>
    <el-table-column prop="hanfuShopDescribe" label="描述"></el-table-column>
    <!-- 操作：1.编辑  2.删除 -->
    <el-table-column label="操作" width="135px">
      <template #default="{ row }">
        <el-button size="small" dark type="primary" @click="handleEdit(row)">编辑</el-button>
        <el-button size="small" dark @click="handleDelete(row)" type="danger">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--分页组件-->
  <el-pagination
      background
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      layout="total, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      style="margin-top: 5px;--el-color-primary: rgb(236,47,27);--el-pagination-button-bg-color: #fff;"
  />
  <!-- 信息表单 -->
  <el-dialog v-model="dialogVisible" title="店铺信息修改" width="500">
    <el-form :model="dialogForm" label-width="auto">
      <el-form-item label="名称">
        <el-input v-model="dialogForm.hanfuShopName" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="dialogForm.hanfuShopDescribe" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button style="display:block;margin:0 auto" type="danger" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import {Search} from '@element-plus/icons-vue'
import {onMounted, ref} from "vue";
import {adminApi} from "../../api/app.js";
import {ElMessage, ElMessageBox} from "element-plus";

// 在组件挂载后执行回调函数
onMounted(() => {
  getData()
})

//定义用户信息数据
const userData = ref([]);
const currentPage = ref(1); //定义当前页码
const pageSize = ref(10); //定义每页显示记录数
const total = ref(0); //总记录数
//获取当前页数据
const getData = () => {
  adminApi.getAllShop({
    pageNum: currentPage.value,
    pageSize: pageSize.value
  }).then(response => {
    userData.value = response.records;
    total.value = response.total;
  })
};

const input = ref(''); // 搜索框输入信息
const select = ref('1'); // 搜索方式选择栏信息

//处理页码变化点击事件
const handleCurrentChange = (pageNum) => {
  currentPage.value = pageNum;
  if(input.value.length > 0) {
    handleSearch();
  } else getData();
};

// 处理搜索
const handleSearch = () => {
  if (input.value.length > 0) { // 输入框有内容，根据内容查询数据
    if(select.value === '1') { // 店铺ID
      adminApi.getShopById({
        pageNum: currentPage.value,
        pageSize: pageSize.value,
        hanfuShopId: input.value
      }).then(response => {
        userData.value = response.records;
        total.value = response.total;
      })
    }  else { // 名称
      adminApi.getShopByName({
        pageNum: currentPage.value,
        pageSize: pageSize.value,
        hanfuShopName: input.value
      }).then(response => {
        userData.value = response.records;
        total.value = response.total;
      })
    }
  } else { // 输入框无内容，显示所有数据
    getData()
  }
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
      '确认要删除该条记录吗？',
      '注意',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    adminApi.deleteShop({
      hanfuShopId: row.hanfuShopId
    }).then(response => {
      ElMessage({ type: 'success',message: '删除成功！' });
      getData();
    }).catch(error => {
      console.error(error);
    });
  }).catch(() => {
    ElMessage({ type: 'info',message: '取消删除。' });
  })
}

const dialogForm = ref({});
const dialogVisible = ref(false);
const dialogType = ref(0);
// 处理编辑
const handleEdit = (row) => {
  dialogType.value = 0;
  dialogForm.value = {...row};
  dialogVisible.value = true;
}

// 处理添加
const handleAdd = () => {
  dialogType.value = 1;
  dialogVisible.value = true;
}

// 确认
const onSubmit = () => {
  if(dialogType.value == 0) { // 编辑
    adminApi.changeShopById(dialogForm.value).then(response => {
      ElMessage({ message: '修改成功！',type: 'success'});
      getData();
      dialogVisible.value = false;
      dialogForm.value = {};
    }).catch(error => {
      console.error(error);
    })
  } else { // 添加
    adminApi.addShop(dialogForm.value).then(response => {
      ElMessage({ message: '添加成功！',type: 'success'});
      getData();
      dialogVisible.value = false;
      dialogForm.value = {};
    }).catch(error => {
      console.error(error);
    })
  }
}
</script>

<style scoped>
.topTool {
  display: flex;
  justify-content: space-between; /* 元素沿着主轴均匀分布 */
  align-items: center; /* 元素在交叉轴上居中对齐 */
  z-index: 0;
  position: relative;
}
.topTool .mt-4 {
  flex: 1; /* 让这个元素占据剩余空间 */
}
table {
  width: 1000px;
  border-collapse: collapse;
  border: 1px solid rgba(0, 0, 0, 0.05);
}
.photo {
  height:180px;
  text-align: center;
}
th,td {
  height: 40px;
  width: 100px;
  border: 1px solid rgba(0, 0, 0, 0.35);
}
th {
  background-color: rgba(198, 226, 255, 0.35);
}
td {
  padding-left: 5px;
}
p {
  margin-top: 5px;
}
.el-pagination :deep(.el-pager li),
.el-pagination :deep(.btn-prev),
.el-pagination :deep(.btn-next){
  border: 1px solid #dcdfe6;
}
</style>